﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyPiano
{
    public partial class PianoKeyboardControl : UserControl
    {
        HashSet<int> spIndex = new HashSet<int>();
        HashSet<int> pressIndex = new HashSet<int>();
        int theme = 0;

        public PianoKeyboardControl()
        {
            InitializeComponent();
            spIndex.Add(2);
            for (int i = 0; i < 7; i++)
            {
                int t = 12 * i;
                spIndex.Add(5 + t);
                spIndex.Add(7 + t);
                spIndex.Add(10 + t);
                spIndex.Add(12 + t);
                spIndex.Add(14 + t);
            }
            
        }

        private void PianoKeyboardControl_SizeChanged(object sender, EventArgs e)
        {
            if (this.Enabled)
                UpdatePianoKey();
        }

        void UpdatePianoKey()
        {
            this.Controls.Clear();

            int pW = (int)(this.Width / 52);
            int pH = this.Height;
            int offset = (int)((this.Width - pW * 52) / 2);

            for (int i = 1, j = 0; i <= 88; i++)
            {
                PianoKeyControl pkc = new PianoKeyControl();
                pkc.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

                if (spIndex.Contains(i))//黑键
                {
                    pkc.Name = "pkc_" + i;
                    pkc.Location = new System.Drawing.Point(offset + j * pW - pW / 4, 0);
                    pkc.OrignColor = System.Drawing.Color.Black;
                    pkc.Size = new System.Drawing.Size(pW / 2, (int)(pH * 0.7));
                    pkc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
                    pkc.Margin = new System.Windows.Forms.Padding(0);
                    pkc.SoundIndex = i;
                    this.Controls.Add(pkc);
                }
                else
                {
                    j++;
                }
                pkc.SetTheme(theme);
            }

            for (int i = 1, j = 0; i <= 88; i++)
            {
                PianoKeyControl pkc = new PianoKeyControl();
                pkc.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

                if (spIndex.Contains(i) == false)//白键
                {
                    pkc.Name = "pkc_" + i;
                    pkc.Location = new System.Drawing.Point(offset + j * pW, 0);
                    pkc.OrignColor = System.Drawing.Color.White;
                    pkc.Size = new System.Drawing.Size(pW, pH);
                    pkc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
                    pkc.Margin = new System.Windows.Forms.Padding(0);
                    pkc.SoundIndex = i;
                    this.Controls.Add(pkc);
                    j++;
                }
                pkc.SetTheme(theme);
            }
        }

        private void PianoKeyboardControl_KeyDown(object sender, KeyEventArgs e)
        {
            if(GlobalData.keyMusicsMap.ContainsKey(e.KeyCode))
            {
                int soundIndex = GlobalData.keyMusicsMap[e.KeyCode] + GlobalData.offset;
                if (soundIndex >= 1 && soundIndex <= 88)
                {
                    PianoKeyControl keyControl = (PianoKeyControl)this.Controls.Find("pkc_" + soundIndex, false)[0];
                    keyControl.UpdateColor();
                }                
            }                 
        }

        private void PianoKeyboardControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (GlobalData.keyMusicsMap.ContainsKey(e.KeyCode))
            {
                int soundIndex = GlobalData.keyMusicsMap[e.KeyCode] + GlobalData.offset;
                if (soundIndex >= 1 && soundIndex <= 88)
                {
                    PianoKeyControl keyControl = (PianoKeyControl)this.Controls.Find("pkc_" + soundIndex, false)[0];
                    keyControl.ResetColor();
                }
            }
        }

        public void SetView(int pianoKeyboardView)
        {
            this.theme = pianoKeyboardView;
            UpdatePianoKey();
        }

        private void PianoKeyboardControl_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.KeyDown += new KeyEventHandler(this.PianoKeyboardControl_KeyDown);
                this.ParentForm.KeyUp += new KeyEventHandler(this.PianoKeyboardControl_KeyUp);
            }
        }

        public void AddPressIndex(int soundIndex)
        {
            pressIndex.Add(soundIndex);
        }

        public void ResetAllPianoKeys()
        {
            if (pressIndex.Count > 0)
            {
                foreach (var index in pressIndex)
                {
                    PianoKeyControl keyControl = (PianoKeyControl)this.Controls.Find("pkc_" + index, false)[0];
                    keyControl.PainoKeyUp();
                }
                pressIndex.Clear();
            }
        }
    }
}
